<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
    <script>
        $(function () {
        //    $("#typeId").select2();//增加下拉框
            show(0);
        })
        function show(id) {
        //    alert("111")
            $.ajax({
                url:"/itemcat/listItemCat",
                type:"post",
                dataType:"json",
                data:{parentId:id},
                success:function (result) {
                if (result.code == 200){
                    var data=result.data;
                    var tbody="";
                    for (var i=0;i<data.length;i++){
                        tbody += '<tr>';
                        tbody += '<td><input  type="checkbox" name="box" value="'+data[i].id+'"></td>';
                        tbody += '<td>'+data[i].id+'</td>';
                        tbody += '<td>'+data[i].name+'</td>';
                        tbody += '<td>'+data[i].typeId+'</td>';
                        tbody += '<td class="text-center">';
                        tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="show('+data[i].id+')">查询下级</button>';
                        tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="getDataById('+data[i].id+')" data-toggle="modal" data-target="#updateModal">修改</button>';
                        tbody += '</td>';
                        tbody += '</tr>';
                    }
                    $("#tbody").html(tbody);
                    //增加父id 增加时回显
                    $("#parentId2").val(id);
                    //修改父id
                    $("#parentId1").val(id);
                    getRank(id);
                }
                },
                error:function (result) {
                    alert("系统异常,请联系管理员");
                }
            })
        }

        function getRank(id) {
          $.ajax({
              url:"/itemcat/getDataById/"+id,
              type:"post",
              data:"json",
              success:function (result) {
                  if (id ==0){
                      $("#"+id+"").nextAll().remove();
                  }else {
                     var data=result.data;
                      var list = $("<li id='" + id + "'><a href='javascript:show(" + id + ")'>" + data.name + "</a></li>");
                      //appendTo() 方法在被选元素的结尾插入指定内容
                      list.appendTo($("#rank"));
                      //根据id删除后面的元素
                      $("#" + id + "").nextAll().remove();
                  }
                  //增加,修改上级商品分类
                  var li = $("#rank").find("li").find("a");
                  var text = "";
                  $.each(li,function (i,e) {
                      if(i == li.length - 1){
                          text += e.text;
                      }else{
                          text += e.text+">>";
                      }
                  })
                  //增加分类
                  $("#pname").html(text);
                  //修改分类
                  $("#updateName").html(text);
              },
              error:function (result) {
                 alert("系统异常,请联系管理员1")
              }
          })

        }
        //删除
        function deleteItemCat() {
            var boxs=$("[name=box]:checked");
            var ids=[];
            $(boxs).each(function (i,e) {
                ids.push(e.value);
            })
            if (ids.length<=0){
                return ;
            }
                $.ajax({
                    url:"/itemcat/deleteIds",
                    type:"post",
                    dataType:"json",
                    data:{ids:ids},
                    success:function (result) {
                        alert(result.message);
                        location.reload();
                    },
                    error:function (result) {
                        alert(result.message);
                    }
                })
        }

        //增加
        function insert() {
            var name=$("#name").val();
            var parentId=$("#parentId2").val();
            alert(parentId);
            var typeId=$("#typeId").val();
            $.ajax({
                url:"/itemcat/addItemCat",
                type:"post",
                dataType:"json",
                data:{name:name,parentId:parentId,typeId:typeId},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }
        //回显
        function getDataById(id) {
            $.ajax({
                url:"/itemcat/updateById",
                type:"post",
                dataType:"json",
                data:{id:id},
                success:function (result) {
                    if (result.code == 200){
                        $("#itemId").val(result.data.id);//内容回显
                        $("#name1").val(result.data.name);
                        $("#typeId1").val(result.data.typeId);
                    }
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }

        //修改
        function updateItemCat() {
            $.ajax({
                url:"/itemcat/updateItemCat",
                type:"post",
                dataType:"json",
                data:{id:$("#itemId").val(),name:$("#name1").val(),typeId:$("#typeId1").val(),parentId:$("#parentId1").val()},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }
    </script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="rank">
                        		<li id="0">
		                        	<a href="javascript:show(0)" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteItemCat()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
                                  <tr>
                                      <td><input type="hidden" ></td>
                                  </tr>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>

		      		<td>上级商品分类</td>
		      		<td>
		      		   <div id="pname"></div>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" placeholder="商品分类名称" id="name">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      			<select name="typeId" id="typeId">
                            <option th:each="temp :${templates}" th:value="${temp.id}">[[${temp.name}]]</option>
                        </select>
                        <input type="hidden" id="parentId2" >
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" >
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 id="myModalLabel1">商品分类编辑</h3>
              </div>
              <div class="modal-body">

                  <table class="table table-bordered table-striped"  width="800px">
                      <tr>
                          <td>上级商品分类</td>
                          <td>
                              <div id="updateName"></div>
                          </td>
                      </tr>
                      <tr>
                          <td>商品分类名称</td>
                          <td><input type="hidden" id="itemId">
                              <input  class="form-control" placeholder="商品分类名称" id="name1">  </td>
                      </tr>
                      <tr>
                          <td>类型模板</td>
                          <td>
                              <select name="typeId" id="typeId1">
                                  <option th:each="temp :${templates}" th:value="${temp.id}">[[${temp.name}]]</option>
                              </select>
                              <input type="hidden" id="parentId1" >
                          </td>
                      </tr>
                  </table>

              </div>
              <div class="modal-footer">
                  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItemCat()">保存</button>
                  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
              </div>
          </div>
      </div>
  </div>

</body>
</html>